###
 # @Date: 2020-10-22 16:02:59
 # @LastEditors: Tonser
 # @LastEditTime: 2020-11-08 05:18:09
 # @FilePath: /SwarmScripts/install.sh
 # @Description: ubuntu 18.04 初步的环境构建
### 
#!/bin/bash

# https://gitee.com/sundx/SwarmScripts/raw/master/install.sh

AutoInstallPath=~/SwarmScripts
CONFIG_PATH=~/SwarmScripts/SwarmConfig
ROS_DISTRO=melodic
SWARM_LIB_PATH=~/SwarmLibs

PASSWD="up"
# 修改源为交大源
echo $PASSWD | sudo -S sed -i 's/archive.ubuntu.com/ftp.sjtu.edu.cn/g' /etc/apt/sources.list
echo $PASSWD | sudo -S sed -i 's/cn.ftp.sjtu.edu.cn/ftp.sjtu.edu.cn/g' /etc/apt/sources.list
echo $PASSWD | sudo -S sed -i 's/cn.archive.ubuntu.com/ftp.sjtu.edu.cn/g' /etc/apt/sources.list

sudo apt-get update
sudo apt install -y git
sudo apt install -y sudo


# 安装code
wget -O code.deb "https://go.microsoft.com/fwlink/?LinkID=760868"
sudo dpkg -i code.deb

 # 安装ros
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.sjtug.sjtu.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sudo apt install -y ros-melodic-desktop-full
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install -y python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
sudo apt install -y python-rosdep
sudo rosdep init
rosdep update

# 安装realsense mavros 
sudo apt-get update 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-realsense2-camera
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-realsense2-description
sudo apt-get install -y --no-install-recommends python-catkin-tools
sudo apt-get install -y --no-install-recommends python-rosinstall-generator
sudo apt-get install -y --no-install-recommends python-rosinstall-generator 
sudo apt-get install -y --no-install-recommends geographiclib-tools
sudo apt-get install -y --no-install-recommends libeigen3-dev 
sudo apt-get install -y --no-install-recommends libgeographic-dev 
sudo apt-get install -y --no-install-recommends libopencv-dev
sudo apt-get install -y --no-install-recommends libyaml-cpp-dev 
sudo apt-get install -y --no-install-recommends protobuf-compiler 
sudo apt-get install -y --no-install-recommends python-pip
sudo apt-get install -y --no-install-recommends python-tk  
sudo apt-get install -y --no-install-recommends tmux 
sudo apt-get install -y --no-install-recommends htop 
sudo apt-get install -y --no-install-recommends nano 
sudo apt-get install -y --no-install-recommends vim 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-serial 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-mav-msgs 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-mavlink 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-mavros 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-mavros-extras 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-ros-base 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-rostest 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-rosunit 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-xacro 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-cv-bridge 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-image-transport 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-message-filters 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-tf 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-joy 
sudo apt-get install -y --no-install-recommends ros-$ROS_DISTRO-octomap-ros 
sudo apt-get install -y --no-install-recommends geographiclib-get-geoids 
sudo apt-get install -y --no-install-recommends egm96-5 
sudo apt-get install -y --no-install-recommends xvfb 
sudo apt-get install -y --no-install-recommends libgoogle-glog-dev
sudo apt-get -y autoremove 
sudo apt-get clean autoclean 
sudo rm -rf /var/lib/apt/lists/{apt,dpkg,cache,log} /tmp/* /var/tmp/*

sudo apt-get update
sudo apt-get install -y --no-install-recommends build-essential 
sudo apt-get install -y --no-install-recommends cmake 
sudo apt-get install -y --no-install-recommends git 
sudo apt-get install -y --no-install-recommends wget 
sudo apt-get install -y --no-install-recommends unzip 
sudo apt-get install -y --no-install-recommends yasm 
sudo apt-get install -y --no-install-recommends pkg-config 
sudo apt-get install -y --no-install-recommends libswscale-dev 
sudo apt-get install -y --no-install-recommends libtbb2 
sudo apt-get install -y --no-install-recommends libtbb-dev 
sudo apt-get install -y --no-install-recommends libjpeg-dev 
sudo apt-get install -y --no-install-recommends libpng-dev 
sudo apt-get install -y --no-install-recommends libtiff-dev 
sudo apt-get install -y --no-install-recommends libavformat-dev 
sudo apt-get install -y --no-install-recommends libpq-dev 
sudo rm -rf /var/lib/apt/lists/*

# python2 useless 
# sudo apt-get install pip
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn pip -U
# pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# pip install --upgrade wheel 'setuptools<45.0.0'
# pip install argcomplete argparse catkin_pkg catkin-tools cerberus coverage 
#     empy jinja2 kiwisolver==1.1.0 matplotlib==2.2.* more-itertools==5.0.0 numpy 
#     pandas==0.24.2 packaging pkgconfig px4tools pygments pymavlink pyros-genmsg 
#     pyulog pyyaml requests rosdep rospkg serial six scipy==0.16 toml


# 设置SSH
sudo apt-get update
sudo apt-get install -y --no-install-recommends openssh-client=1:7.6p1-4
sudo apt-get install -y --no-install-recommends openssh-server
sudo service ssh start
# sudo echo "PermitRootLogin yes">>/etc/ssh/sshd_config
echo "PermitRootLogin yes" |sudo tee -a /etc/ssh/sshd_config
sudo systemctl enable ssh



# install codes
./auto_clone_codes.sh
echo $PASSWD | sudo -S ./install_geo.sh

# install libs 
# 注意 opencv 编译时间较长,此处使用ros内置opencv ,暂不编译
cd $SWARM_LIB_PATH
# OPENCV_VERSION="3.4.0"
# unzip opencv_contrib-$OPENCV_VERSION.zip 
# rm opencv_contrib-$OPENCV_VERSION.zip 
# unzip opencv-$OPENCV_VERSION.zip 
# rm opencv-$OPENCV_VERSION.zip 
# cd opencv-$OPENCV_VERSION
# mkdir build
# cd build
# cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DOPENCV_EXTRA_MODULES_PATH=$SWARM_LIB_PATH/opencv_contrib-${OPENCV_VERSION}/modules -DWITH_CUDA=OFF -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE .. 
# sudo make install 
# cd ..
# cd ..
unzip lcm-1.4.0.zip 
cd lcm-1.4.0  
mkdir build 
cd build 
cmake .. 
make 
echo $PASSWD | sudo -S make install 
echo "source ~/swarm_ws/devel/setup.bash" >>~/.bashrc
source ~/.bashrc
echo $PASSWD | sudo -S ./install_geo.sh

./../set/set_mode_ttyUSB.sh
./../set/set_realsense.sh
./../set/setting_ssh.sh

cd ~/swarm_ws
catkin_make

  
